<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<mat-card>
  <mat-card-content class="settings">
    <h3 class="settings__title">Settings</h3>
    <div class="settings__subtitle">Adjust the settings to match your needs.</div>

    <form *ngIf="form" [formGroup]="form">
      <gio-banner-info>
        <div>Logging smartly</div>
        <span gioBannerBody>Opt for logging checkboxes carefully, this requires extra storage and may affect API performance.</span>
      </gio-banner-info>

      <div class="settings__form-control">
        <div class="settings__form-control__title">Logging mode</div>
        <div class="settings__form-control__subtitle">Customize the mode included.</div>
        <mat-checkbox formControlName="entrypoint">Entrypoint</mat-checkbox>
        <mat-checkbox formControlName="endpoint">Endpoint</mat-checkbox>
      </div>

      <div class="settings__form-control">
        <div class="settings__form-control__title">Logging phase</div>
        <div class="settings__form-control__subtitle">Customize the phases included.</div>
        <mat-checkbox formControlName="request">Request</mat-checkbox>
        <mat-checkbox formControlName="response">Response</mat-checkbox>
      </div>

      <div class="settings__form-control">
        <div class="settings__form-control__title">Content data</div>
        <div class="settings__form-control__subtitle">Customize the data included.</div>
        <p class="settings__form-control__info" *ngIf="loggingModeDisabled">
          <mat-icon svgIcon="gio:info"></mat-icon> To select an option, ensure you’ve checked one mode or more above.
        </p>
        <mat-checkbox formControlName="messageContent">Message content</mat-checkbox>
        <mat-checkbox formControlName="messageHeaders">Message headers</mat-checkbox>
        <mat-checkbox formControlName="messageMetadata">Message metadata</mat-checkbox>
        <mat-checkbox formControlName="headers">Headers</mat-checkbox>
      </div>

      <div class="settings__form-control">
        <div class="settings__form-control__title">Message sampling</div>
        <div class="settings__form-control__subtitle">Set a personalized sampling configuration.</div>
        <mat-button-toggle-group aria-label="Select message sampling typ" class="sampling-type" formControlName="samplingType">
          <mat-button-toggle class="sampling-type__toggle" value="PROBABILITY"> Probabilistic </mat-button-toggle>
          <mat-button-toggle class="sampling-type__toggle" value="COUNT"> Count </mat-button-toggle>
          <mat-button-toggle class="sampling-type__toggle" value="TEMPORAL"> Temporal </mat-button-toggle>
        </mat-button-toggle-group>

        <ng-container *ngIf="samplingType === 'PROBABILITY'">
          <p>Samples messages based on a specified probability.</p>
          <p>The value should be between 0.01 and {{ settings?.logging.messageSampling.probabilistic.limit ?? 0.5 }}.</p>
          <mat-form-field data-testid="sampling_value" class="settings__form-control__form-field">
            <mat-label>Message sampling</mat-label>
            <input matInput type="number" formControlName="samplingValue" />
            <mat-error *ngIf="form.get('samplingValue').hasError('required')"> The sampling value is required. </mat-error>
            <mat-error *ngIf="form.get('samplingValue').hasError('min')"> The sampling value should be greater than 0.01. </mat-error>
            <mat-error *ngIf="form.get('samplingValue').hasError('max')">
              The sampling value should be lower than {{ settings?.logging.messageSampling.probabilistic.limit ?? 0.5 }}.
            </mat-error>
          </mat-form-field>
        </ng-container>

        <ng-container *ngIf="samplingType === 'COUNT'">
          <p>Samples one message for every number of specified messages.</p>
          <p>The value should be greater than {{ settings?.logging?.messageSampling?.count?.limit ?? 10 }}.</p>
          <mat-form-field data-testid="sampling_value" class="settings__form-control__form-field">
            <mat-label>Message sampling</mat-label>
            <input matInput type="number" formControlName="samplingValue" />
            <mat-error *ngIf="form.get('samplingValue').hasError('required')"> The sampling value is required. </mat-error>
            <mat-error *ngIf="form.get('samplingValue').hasError('min')">
              The sampling value should be greater than {{ settings?.logging?.messageSampling?.count?.limit ?? 10 }}.
            </mat-error>
          </mat-form-field>
        </ng-container>

        <ng-container *ngIf="samplingType === 'TEMPORAL'">
          <p>Samples messages based on time duration.</p>
          <p>
            The value should conform to ISO-8601 duration format, e.g. PT1S for a duration of 1 second and be greater than
            {{ settings?.logging?.messageSampling?.temporal?.limit ?? 'PT1S' }}.
          </p>
          <mat-form-field data-testid="sampling_value" class="settings__form-control__form-field">
            <mat-label>Message sampling</mat-label>
            <input matInput formControlName="samplingValue" />
            <mat-error *ngIf="form.get('samplingValue').hasError('required')"> The sampling value is required. </mat-error>
            <mat-error *ngIf="form.get('samplingValue').hasError('invalidISO8601Duration')">
              The sampling value should use ISO-8601 duration format, e.g.
              {{ settings?.logging?.messageSampling?.temporal?.limit ?? 'PT1S' }}.
            </mat-error>
            <mat-error *ngIf="form.get('samplingValue').hasError('minTemporal')">
              The sampling value should be greater than {{ settings?.logging?.messageSampling?.temporal?.limit ?? 'PT1S' }}.
            </mat-error>
          </mat-form-field>
        </ng-container>
      </div>

      <div class="settings__form-control">
        <div class="settings__form-control__title">Display conditions</div>
        <div class="settings__form-control__subtitle">Filter the data that matter to you.</div>

        <mat-form-field>
          <mat-label>Request phase condition</mat-label>
          <input matInput formControlName="requestCondition" />
          <mat-hint> Support EL (e.g: &#123; #request.headers['Content-Type'][0] === 'application/json' &#125;)</mat-hint>
        </mat-form-field>

        <mat-form-field class="settings__form-control__form-field">
          <mat-label>Message condition</mat-label>
          <input matInput formControlName="messageCondition" />
          <mat-hint> Support EL (e.g: &#123; #message.headers['Content-Type'][0] === 'application/json' &#125;)</mat-hint>
        </mat-form-field>
      </div>

      <gio-save-bar *ngIf="form.dirty" [form]="form" [formInitialValues]="initialFormValue" (submitted)="save()"></gio-save-bar>
    </form>
  </mat-card-content>
</mat-card>
